home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / picture / project.c < prev    next >
Text File  |  1993-09-23  |  5KB  |  178 lines

  1. //    Copyright 1993 Ralph Gonzalez
  2.  
  3. /*
  4. *    FILE:        project.c
  5. *    AUTHOR:        R. Gonzalez
  6. *    CREATED:    October 3, 1990
  7. *
  8. *    methods for projector class, which displays 2D images on screen
  9. */
  10.  
  11. # include    "project.h"
  12. # include    "error.h"
  13. # include    <stdlib.h>
  14.  
  15. extern Error    *gerror;
  16. static int        old_window_num = -1;
  17. static double    old_c2_x;
  18. static double    old_c2_y;
  19.  
  20. /******************************************************************
  21. *    initialize.
  22. ******************************************************************/
  23. Projector::Projector(void)
  24. {
  25.     cropping_frame = new Frame;
  26.     projection_frame = new Frame;
  27.     window_num = -1;
  28.     window_frame = NULL;
  29.     screen_ptr = NULL;
  30.     
  31. //    derived projectors may change following initializations: 
  32.  
  33.     set_cropping_frame(0.,0.,1.,1.);
  34.     set_projection_frame(0.,0.,1.,.1);
  35.     set_background_color(BLACK);
  36. }
  37.  
  38. /******************************************************************
  39. *    set background color - duh!  (Default is set in constructor.)
  40. ******************************************************************/
  41. void    Projector::set_background_color(color bck_color_val)
  42. {
  43.     background_color = bck_color_val;
  44. }
  45.  
  46. /******************************************************************
  47. *    set cropping frame to indicate what part of 2D space will
  48. *    be drawn.  Otherwise the default will be used.  In 3D case,
  49. *    use coordinates of focal plane of camera.
  50. ******************************************************************/
  51. void    Projector::set_cropping_frame(double x,double y,
  52.                                     double width,double height)
  53. {
  54.     cropping_frame->set(x,y,width,height);
  55. }
  56.  
  57. /******************************************************************
  58. *    set projection frame to indicate where the window will appear
  59. *    on the screen.  This must be called before set_screen(), if
  60. *    at all!  Use normalized screen coordinates.
  61. ******************************************************************/
  62. void    Projector::set_projection_frame(double x,double y,
  63.                                     double width,double height)
  64. {
  65.     projection_frame->set(x,y,width,height);
  66. }
  67.  
  68. /******************************************************************
  69. *    set pointer to screen used for projection, and allocate a
  70. *    window on the screen.  This must be called before clearing
  71. *    or drawing!  Do not call this more than once!
  72. ******************************************************************/
  73. void    Projector::set_screen(Generic_Screen* screen_ptr_val)
  74. {
  75.     if (screen_ptr != NULL)
  76.         gerror->report("Screen already set");
  77.     else
  78.     {
  79.         screen_ptr = screen_ptr_val;
  80.         window_num = screen_ptr->new_window(projection_frame);
  81.         window_frame = new Frame;
  82.         screen_ptr->get_window_device_frame(window_num,window_frame);
  83.         clear();
  84.     }
  85. }
  86.  
  87. /******************************************************************
  88. *    clear window using background color
  89. ******************************************************************/
  90. void    Projector::clear(void)
  91. {
  92.     if (screen_ptr == NULL)
  93.         gerror->report("Can't clear() with no screen");
  94.     else
  95.     {
  96.         screen_ptr->set_current_window(window_num);
  97.         screen_ptr->set_pen_color(background_color);
  98.         screen_ptr->fill_window();
  99.         old_window_num = -1;
  100.     }
  101. }
  102.  
  103. /******************************************************************
  104. *    make corresponding window closest
  105. ******************************************************************/
  106. void    Projector::overlap(void)
  107. {
  108.     if (screen_ptr == NULL)
  109.         gerror->report("Can't overlap() with no screen");
  110.     else
  111.         screen_ptr->make_closest(window_num);
  112. }
  113.  
  114. /******************************************************************
  115. *    show line on screen.  If the window hasn't changed and the
  116. *    new line continues where the previous one left off, then
  117. *    drawing may be faster.  2D world coordinates are used (in 3D,
  118. *    those of camera focal plane).
  119. ******************************************************************/
  120. void    Projector::show_line(Coord2 *c1,Coord2 *c2,color line_color)
  121. {
  122.     Coord2    *c3,
  123.             *c4;
  124.             
  125.     if (screen_ptr == NULL)
  126.         gerror->report("Can't show_line() with no screen");
  127.     else
  128.     {
  129.         if (old_window_num == window_num &&
  130.             old_c2_x == c1->x && old_c2_y == c1->y)
  131.         {
  132.             screen_ptr->set_pen_color(line_color);
  133.             c4 = new Coord2;
  134.             c4->convert(c2,cropping_frame,window_frame);
  135.             screen_ptr->draw_to(c4);
  136.             delete c4;
  137.         }
  138.         else
  139.         {
  140.             screen_ptr->set_current_window(window_num);
  141.             screen_ptr->set_pen_color(line_color);
  142.             c3 = new Coord2;
  143.             c3->convert(c1,cropping_frame,window_frame);
  144.             c4 = new Coord2;
  145.             c4->convert(c2,cropping_frame,window_frame);
  146.             screen_ptr->draw_line(c3,c4);
  147.             delete c3;
  148.             delete c4;
  149.         }
  150.         
  151.         old_window_num = window_num;
  152.         old_c2_x = c2->x;
  153.         old_c2_y = c2->y;
  154.     }
  155. }
  156.  
  157. /******************************************************************
  158. *    destroy
  159. ******************************************************************/
  160. Projector::~Projector(void)
  161. {
  162.     delete cropping_frame;
  163.     delete projection_frame;
  164.     if (window_frame != NULL)
  165.         delete window_frame;
  166. }
  167.  
  168. /******************************************************************
  169. *    initialize corner projector
  170. ******************************************************************/
  171. Corner_Projector::Corner_Projector(void)
  172. {
  173.     set_cropping_frame(0.,0.,2.,2.);
  174.     set_projection_frame(.6,-.4,.4,.4);
  175.     set_background_color(MAGENTA);
  176. }
  177.  
  178.